package com.chaos.auth.config;

import com.chaos.auth.config.granter.admin.AdminByUsernameAuthenticationProvider;
import com.chaos.auth.config.granter.user.UserByAliAuthenticationProvider;
import com.chaos.auth.config.granter.user.UserByPhoneAuthenticationProvider;
import com.chaos.auth.config.granter.admin.AdminByPhoneAuthenticationProvider;
import com.chaos.auth.config.granter.user.UserByWxAuthenticationProvider;
import com.chaos.auth.service.userdetails.AdminByPhoneDetailsService;
import com.chaos.auth.service.userdetails.AdminByUsernameDetailsService;
import com.chaos.framework.model.serivce.RedisService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * @author Jacky
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private AdminByUsernameDetailsService adminByUsernameDetailsService;
    @Resource
    private AdminByPhoneDetailsService adminByPhoneDetailsService;
    @Resource
    private UserByPhoneAuthenticationProvider userByPhoneAuthenticationProvider;
    @Resource
    private UserByWxAuthenticationProvider userByWxAuthenticationProvider;
    @Resource
    private UserByAliAuthenticationProvider userByAliAuthenticationProvider;
    @Resource
    private RedisService redisService;

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable();
        http.authorizeRequests().antMatchers("/**").permitAll();
        http.formLogin();
        http.authorizeRequests().anyRequest().authenticated();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) {
        auth
                .authenticationProvider(adminByUsernameAuthenticationProvider())
                .authenticationProvider(adminByPhoneAuthenticationProvider())
                .authenticationProvider(userByPhoneAuthenticationProvider)
                .authenticationProvider(userByWxAuthenticationProvider)
                .authenticationProvider(userByAliAuthenticationProvider)
        ;
    }

    @Bean
    public AdminByUsernameAuthenticationProvider adminByUsernameAuthenticationProvider() {
        AdminByUsernameAuthenticationProvider provider = new AdminByUsernameAuthenticationProvider();
        provider.setUserDetailsService(adminByUsernameDetailsService);
        provider.setPasswordEncoder(passwordEncoder());
        provider.setRedisService(redisService);
        return provider;
    }

    @Bean
    public AdminByPhoneAuthenticationProvider adminByPhoneAuthenticationProvider() {
        AdminByPhoneAuthenticationProvider provider = new AdminByPhoneAuthenticationProvider();
        provider.setUserDetailsService(adminByPhoneDetailsService);
        return provider;
    }

}
